package com.wu.llpsw.yuanli.energy;

import com.wu.llpsw.networking.ModMessage;
import com.wu.llpsw.networking.packet.backYuanLiMaxPacket;
import com.wu.llpsw.networking.packet.backYuanLiNowPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.util.LazyOptional;

/**
 * 操作元力
 */
public class yuanliManager {
    static int now=10;
    static int max=10;
    static int out=10;
    static int back=0;
    //修改当前元力的值
    public static int use(Player player, int value){
        LazyOptional<power> speedCap = player.getCapability(CapabilityRegistryHandler.POWER);
        speedCap.ifPresent((l) -> {
            int nowPower = l.getNowPower();
            if (nowPower+value>=1){
                if((nowPower+value)<=l.getMaxPower()){
                    l.setNowPower(l.getNowPower()+value);
                    back=value;
                }
                else {
                    l.setNowPower(l.getMaxPower());
                    back=l.getMaxPower()-nowPower;
                }
            }
            else if(nowPower+value<1){
                l.setNowPower(1);
                back=nowPower-1;
            }
                }
        );
        ModMessage.sendToPlayer(new backYuanLiMaxPacket(yuanliManager.getMaxYuanLi(player)),(ServerPlayer) player);
        ModMessage.sendToPlayer(new backYuanLiNowPacket(yuanliManager.getNowYuanLi(player)),(ServerPlayer)player);
        return back;
    }
    //设置玩家最大元力
    public static void setMax(Player player,int value){
        LazyOptional<power> speedCap = player.getCapability(CapabilityRegistryHandler.POWER);
        speedCap.ifPresent((l) -> {
            if(l.getNowPower()>value){
                l.setNowPower(value);
            }
            l.setMaxPower(value);
                }
        );
        //ModMessage.sendToPlayer(new backYuanLiMaxPacket(yuanliManager.getMaxYuanLi(player)),(ServerPlayer) player);
        //ModMessage.sendToPlayer(new backYuanLiNowPacket(yuanliManager.getNowYuanLi(player)),(ServerPlayer)player);
    }


    public static int getMaxYuanLi(Player player){
        LazyOptional<power> speedCap = player.getCapability(CapabilityRegistryHandler.POWER);
        speedCap.ifPresent((l) -> {
                    max=l.getMaxPower();
                }
        );
        return max-1;
    }

    public static int getNowYuanLi(Player player){
        LazyOptional<power> speedCap = player.getCapability(CapabilityRegistryHandler.POWER);
        speedCap.ifPresent((l) -> {
                    now=l.getNowPower();
                }
        );
        return now-1;
    }
}
